package com.example.itcompanyinterview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.example.itcompanyinterview.object.Category;
import com.example.itcompanyinterview.tools.AboutDialog;
import com.example.itcompanyinterview.tools.ClientUpdateManager;
import com.example.itcompanyinterview.tools.CustomTitleSetter;
import com.example.itcompanyinterview.tools.ExitHelper;
import com.example.itcompanyinterview.tools.TitleBarGenerator;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class GridScreenActivity extends Activity {
	private GridView _gridView1;
	private CustomTitleSetter _titleBar;
	private ExitHelper exitHelper = new ExitHelper();
	private ClientUpdateManager updateManager;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
		this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_grid_screen);
        _titleBar = new CustomTitleSetter(this);
        _titleBar.setTitle("破解IT面试", View.GONE, View.GONE);
		this.initApp();
		this.initMainScreen();
	}

	private void initMainScreen() {
        _gridView1 = (GridView) findViewById(R.id.screenGridView);
        int[] ItemImageArray = {R.drawable.fundemental, R.drawable.prepare, R.drawable.algorithm,
        						R.drawable.experience, R.drawable.googleglass, R.drawable.feedback,
        						R.drawable.info};
        Resources res = getResources();
        String[] ItemImageTitle=res.getStringArray(R.array.main_griditem_title);
        List<HashMap<String, Object>> lst = new ArrayList<HashMap<String, Object>>();  
        for (int i = 0; i < ItemImageArray.length; i++) {  
            HashMap<String, Object> map = new HashMap<String, Object>();
            if(ItemImageArray[i] == R.drawable.googleglass) {	//skip this one
            	continue;
            }
            map.put("itemImage", ItemImageArray[i]);  
            map.put("itemText", ItemImageTitle[i]);  
            lst.add(map);  
        }  
  
        SimpleAdapter adpter = new SimpleAdapter(this, lst,  
                R.layout.main_gridview_item, // 指定 子项的布局文件的ID  
                new String[] { "itemImage", "itemText" }, // 指定 数据的列  
                new int[] { R.id.imageView_ItemImage, R.id.textView_ItemText });// 指定数据的列对应到的  
                                                                                // 绑定的目标控件  
        _gridView1.setAdapter(adpter);  
        _gridView1.setOnItemClickListener(onclickcistener);  
    }
  
    private OnItemClickListener onclickcistener = new OnItemClickListener() {  
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
                long arg3) {
        	HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
        	int id = arg2;
        	String title = (String)item.get("itemText");
            switch(id) {
            	case 0:
            	case 1:
            	case 2:
            	case 3:
            		Bundle bundle = new Bundle();
    				bundle.putSerializable("category", new Category(id + 1, title));
            		Intent intent = new Intent(GridScreenActivity.this, SubCategoryActivity.class);
            		intent.putExtras(bundle);
            		startActivity(intent);
    				break;
            	case 4:
            		startActivity(new Intent(GridScreenActivity.this, FeedBackActivity.class));
            		break;
            	case 5:
            		AboutDialog about = new AboutDialog(GridScreenActivity.this);
    				about.setTitle("关于");
    				about.show();
    				break;
            }
        }  
    };
    
    @Override
	public void onBackPressed() {
		if(this.exitHelper.isExit()) {
			this.finish();
		} else {
			Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
			this.exitHelper.doExitInOneSecond();
		}
	}
    
    private void initApp() {
		if(this.isNetworkConnected() == false) {
        	this.alertDisconnectDialogAndExit();
        }
		this.updateManager = new ClientUpdateManager(this);
    	this.updateManager.checkUpdate();
	}
    
    private boolean isNetworkConnected() {
    	ConnectivityManager conMgr =  (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    	NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
    	if (activeNetwork != null && activeNetwork.isConnected()) {
    	    return true;
    	} else {
    	    return false;
    	} 
    }
	
    private void alertDisconnectDialogAndExit() {
    	AlertDialog alert = new AlertDialog.Builder(this).create();
    	alert.setTitle("没有可用的网络");
    	alert.setMessage("请开启GRPS或WIFI网络连接");
    	alert.setButton(DialogInterface.BUTTON_POSITIVE, "确认", 
    			new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						System.exit(0);
					}
				}
    	);
    	alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", 
    			new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						System.exit(0);
						
					}
				}
    	);
    	alert.show();
    }

}
